package com.sgb.kjwl.viewmodel.manage;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.UrlConfig;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.TwoLevelChildForPopWindowEntity;
import com.sgb.kjwl.model.entity.TwoLevelForPopWindowEntity;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EnterpriseAndMapModel extends AndroidViewModel {

    private int page = 1;
    private List<PickSelectEntity> list;
    public MutableLiveData<List<PickSelectEntity>> mCapital = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> mDistance = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> mType = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mCity = new MutableLiveData<>();
    public MutableLiveData<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>> mTradeType = new MutableLiveData<>();
    public MutableLiveData<Boolean> mFollow = new MutableLiveData<>();
    public MutableLiveData<List<TheEnterpriseOfEntity>> mCom = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity<List<PickSelectEntity>>>> mTypeFirst = new MutableLiveData<>();

    public EnterpriseAndMapModel(@NonNull @NotNull Application application) {
        super(application);
    }

    //获取注册资金
    public void findCapitalList(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getPersonNumber().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    List<PickSelectEntity> list = response.body().getData();
                    mCapital.postValue(list);
                } else {
                    MToast.showLongToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取距离
    public void getDistanceDatas() {
        if (list == null) {
            list = new ArrayList<>();
            PickSelectEntity entity1 = new PickSelectEntity("1000000", "不限");
            PickSelectEntity entity2 = new PickSelectEntity("10", "10KM");
            PickSelectEntity entity3 = new PickSelectEntity("20", "20KM");
            PickSelectEntity entity4 = new PickSelectEntity("30", "30KM");
            PickSelectEntity entity5 = new PickSelectEntity("40", "40KM");
            PickSelectEntity entity6 = new PickSelectEntity("50", "50KM");
            PickSelectEntity entity7 = new PickSelectEntity("60", "60KM");
            PickSelectEntity entity8 = new PickSelectEntity("70", "70KM");
            list.add(entity1);
            list.add(entity2);
            list.add(entity3);
            list.add(entity4);
            list.add(entity5);
            list.add(entity6);
            list.add(entity7);
            list.add(entity8);
        }
        mDistance.postValue(list);
    }

    //获取列表数据
    public void getComList(Map<String,Object> map,Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getComList(map).enqueue(new Callback<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>> call, Response<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    mCom.postValue(response.body().getData().getList());
                } else {
                    MToast.showLongToast(context, "数据异常");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getType(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getType().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    List<PickSelectEntity> list = response.body().getData();
                    list.add(0,new PickSelectEntity("","全部"));
                    mType.postValue(list);
                } else {
                    MToast.showLongToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getCityData(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mCity.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getTradeTypes(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getCompanyTradeType().enqueue(new Callback<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> call, Response<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mTradeType.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void follow(String shopId,Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().userFocusCompany(shopId).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollow.postValue(true);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getTypeList(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Response<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity<List<PickSelectEntity>>> list = new ArrayList<>();
                    list.add(new PickSelectEntity("","全部"));
                    list.addAll(response.body().getData());
                    mTypeFirst.postValue(list);
                } else {
                    MToast.showLongToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
